package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkFieldContent;
import com.zb.mapper.ZjWorkFieldContentMapper;
import com.zb.service.ZjWorkFieldContentService;
import com.zb.vo.WorkFieldContentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单字段内容表，该表存储字段类型为：单选、多选的内容 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-09-26
 */
@Service
public class ZjWorkFieldContentServiceImpl extends ServiceImpl<ZjWorkFieldContentMapper, ZjWorkFieldContent> implements ZjWorkFieldContentService {

    // 保存、修改、删除字段内容
    @Override
    public void save(Long custAccId, Long workFieldId, List<WorkFieldContentVo> contentVos, List<Long> ids) {
        if (null != ids && !ids.isEmpty()) {
            this.removeByIds(ids);
        }
        if (null == contentVos || contentVos.isEmpty()) {
            return;
        }
        Assert.isTrue(contentVos.stream().map(WorkFieldContentVo::getVal).toList().size() == contentVos.stream().map(WorkFieldContentVo::getVal).collect(Collectors.toSet()).size(),
                "单选/多选内容已重复，请检查后重试");
        List<ZjWorkFieldContent> adds = new ArrayList<>();
        List<ZjWorkFieldContent> updates = new ArrayList<>();
        ZjWorkFieldContent content = null;
        for (WorkFieldContentVo vo : contentVos) {
            content = new ZjWorkFieldContent();
            BeanUtils.copyProperties(vo, content);
            if (null != content.getId()) {
                updates.add(content);
                continue;
            }
            content.setId(IdWorker.getId());
            content.setCustAccId(custAccId);
            content.setWorkFieldId(workFieldId);
            adds.add(content);
        }
        if (!adds.isEmpty()) this.saveBatch(adds);
        if (!updates.isEmpty()) this.updateBatchById(updates);
    }

    // 根据工单字段表ID查询单选/多选内容
    @Override
    public List<ZjWorkFieldContent> getByWorkFieldIds(List<Long> workFieldIds) {
        if (null == workFieldIds || workFieldIds.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_field_id", "val");
        wrapper.in("work_field_id", workFieldIds);
        return this.list(wrapper);
    }

    // 根据工单字段表ID查询单选/多选内容对象
    @Override
    public List<ZjWorkFieldContent> getByWorkFieldId(Long workFieldId) {
        QueryWrapper<ZjWorkFieldContent> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_field_id", "val");
        wrapper.eq("work_field_id", workFieldId);
        return this.list(wrapper);
    }

    // 根据工单字段表ID查询单选/多选内容删除
    @Override
    public void deleteBatchByWorkFieldIds(List<Long> workFieldIds) {
        if (null == workFieldIds || workFieldIds.isEmpty()) {
            return;
        }
        QueryWrapper<ZjWorkFieldContent> wrapper = new QueryWrapper<>();
        wrapper.in("work_field_id", workFieldIds);
        this.remove(wrapper);
    }
}
